home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / SpellFile.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  4.7 KB  |  156 lines

  1. /*    SpellFile 
  2.     - class contains all methods necessary to carry out all transactions between
  3.         the file system and the SpellBook application.
  4.         
  5.     05.01.99 by Scott C. Ziegler for use with AD&D and Simulcra RPG.
  6.     
  7.     vers. history:
  8.         
  9.         05.23.00 - Illuminatus! Day. Moved from 2 methods to 4 by abstracting the actual
  10.                     disk operations code from the JFileChooser display.  This allows for
  11.                     calls to save the state of the spellbook without prompting the user.
  12.                     Benefits include default/autosave capability.
  13.         06.23.00 - Illuminatus! Day. achieved syntacticallly correct version. (@ MacHack)
  14.         02.19.01 - changed some parameter interactions such as load doesn't need a spellbook param
  15.     
  16.     Work Needed:
  17.         - !!! Major Error in Design: SpellBook, when updated will invalidate previously
  18.             serialized SpellBooks of earlier version.  It is smarter to have a special
  19.             format that writes several java objects like ints, vectors, & strings.
  20.         - need to make a backup method that keeps a separate record, char based file (gzipped)
  21.         - add more accessory functions like wrappered autosaves, etc.
  22.         - add import function which takes a tab-delineated text file and converts
  23.             it to a SpellRecord
  24.         
  25.     Work Completed:
  26.         
  27.         - added the parent parameter for save as and load from so FileChooser can show dialog.
  28.         - changed load parameters
  29.     
  30. */
  31.  
  32. /*
  33. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  34. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  35. */
  36.  
  37. package Arcana;
  38.  
  39. import java.io.*;
  40. import java.util.*;
  41. import java.util.zip.*;
  42. import java.awt.*;
  43. import javax.swing.*;
  44. import javax.swing.JFileChooser;
  45.  
  46. public class SpellFile {
  47.  
  48.     static final String DEFAULT_FILENAME = "untitled.spellbook";
  49.  
  50.     File      spellFile;
  51.     String    currentPath;
  52.     
  53.     SpellBook    storedBook;
  54.     
  55.     public SpellFile() {
  56.         spellFile = new File(DEFAULT_FILENAME);
  57.         storedBook = new SpellBook();
  58.         }
  59.     
  60.     public SpellFile(String path) {
  61.         spellFile = new File(path);
  62.         currentPath = path;
  63.         storedBook = new SpellBook();
  64.         }
  65.     
  66.     // Methods:
  67.     
  68.     public void setPath(String path) {
  69.         currentPath = path;
  70.         }
  71.         
  72.     public String getPath() {
  73.         return currentPath;
  74.         }
  75.     
  76.     public SpellBook load() {
  77.         
  78.     //    if (currentPath.compareTo("") == 0) load(DEFAULT_FILENAME);
  79.     //    else load(currentPath);
  80.         
  81.         return load(((currentPath.compareTo("") == 0)?DEFAULT_FILENAME:currentPath));
  82.         }
  83.     
  84.     public SpellBook load(String filename) {
  85.         Vector readVector = new Vector();
  86.         if (filename != null) {
  87.             try {
  88.                 FileInputStream fis = new FileInputStream(filename);
  89.                 //GZIPInputStream gzis = new GZIPInputStream(fis);
  90.                 //ObjectInputStream in = new ObjectInputStream(gzis);
  91.                 ObjectInputStream in = new ObjectInputStream(fis);
  92.                 
  93.                 readVector = (Vector)in.readObject();
  94.                 storedBook = (SpellBook)readVector.elementAt(0);
  95.                 in.close();
  96.                 return storedBook;
  97.                 
  98.                 }
  99.             catch (IOException ie) {
  100.                 System.out.println("Error -- " + ie.toString());
  101.                 return storedBook = new SpellBook();
  102.                 }
  103.             catch (ClassNotFoundException cnfe) {
  104.                 return storedBook = new SpellBook();
  105.                 }
  106.             }
  107.         else {
  108.             // throw a JDialog w/ error. (actually JOptionPane)
  109.             return storedBook = new SpellBook();
  110.             }
  111.         }
  112.         
  113.     public void save(SpellBook spellBook) {
  114.         if (currentPath.compareTo("") == 0) save(DEFAULT_FILENAME, spellBook);
  115.         else save(currentPath, spellBook);
  116.         }
  117.     
  118.     public void save(String filename, SpellBook spellBook) {
  119.         Vector writeVector = new Vector();
  120.         if (filename != null) {
  121.             try {
  122.                 FileOutputStream fos = new FileOutputStream(filename);
  123.                 //GZIPOutputStream gzos = new GZIPOutputStream(fos);
  124.                 //ObjectOutputStream out = new ObjectOutputStream(gzos);
  125.                 ObjectOutputStream out = new ObjectOutputStream(fos);
  126.                 
  127.                 writeVector.addElement(spellBook);
  128.                 out.writeObject(writeVector);
  129.                 out.flush();
  130.                 out.close();
  131.                 }
  132.             catch (IOException ie) {
  133.                 System.out.println("Error -- " + ie.toString());
  134.                 }
  135.             }
  136.         else {
  137.             // throw a JDialog w/ error. (actually JOptionPane)
  138.             }
  139.         }
  140.     
  141.     public SpellBook loadFrom(Component parent) {
  142.         JFileChooser f = new JFileChooser();
  143.         f.showOpenDialog(parent);
  144.         String filename = f.getName();
  145.         return load(filename); 
  146.         }
  147.         
  148.     public void saveAs(SpellBook spellBook, Component parent) {
  149.         JFileChooser f = new JFileChooser();
  150.         f.setDialogType(JFileChooser.SAVE_DIALOG);
  151.         f.setDialogTitle("Save Spells");
  152.         f.showSaveDialog(parent);
  153.         String filename = f.getSelectedFile().getName();
  154.         save(filename, spellBook);
  155.         }
  156.     }